package com.example.ailiyuncloud.service.Impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ailiyuncloud.dto.CommodityDto;
import com.example.ailiyuncloud.dto.InventoryAddDto;
import com.example.ailiyuncloud.mapper.CommodityMapper;
import com.example.ailiyuncloud.pojo.BaseResponseData;
import com.example.ailiyuncloud.pojo.Result;
import com.example.ailiyuncloud.service.CommodityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: lqj
 * @Date: 2024/05/23/14:36
 * @Description:
 */
@Service
public class CommodityServiceImpl implements CommodityService {


    @Autowired
    private CommodityMapper commodityMapper;

    @Autowired
    private RedisTemplate<String, Object> stringRedisTemplate;


    @Override
    public Result<String> addCommodityService(List<CommodityDto> commodityDto) {
        // TODO 后续在此加入分布式锁
        Map<String, Object> map = new HashMap<>(16);
        commodityDto.forEach(commodity -> {
            commodity.setCommodityId(System.currentTimeMillis());
            commodityMapper.insert(commodity);
        });
        // TODO 后续在此加入分布式锁
        return Result.OK("商品添加成功");
    }

    @Override
    public Result<Map<String, Object>> QueryCommodityService(Page page, CommodityDto commodityDto) {
        // TODO 后续在此加入先查询缓存 如缓存中不存在再查数据库
        String key = "commodity";
        Map<String, Object> map = new HashMap<>(16);
        IPage<CommodityDto> commodityList = commodityMapper.getCommodityList(page, commodityDto);
        stringRedisTemplate.opsForValue().set(key, commodityList.getRecords());
        Map<Object, Object> entries = stringRedisTemplate.opsForHash().entries(key);
        map.put("result", entries.values());
        map.put("page", commodityList.getCurrent());
        map.put("size", commodityList.getSize());
        stringRedisTemplate.delete(key);
        return Result.OK(map);
    }

    @Override
    public Result<Map<String, Object>> inventoryAddService(List<InventoryAddDto> list) {
        //todo 库存目前不做上限控制
        //todo 先查缓存中是否存在该商品id，如不存在查询数据库。数据库不存在返回报错
        //todo 后续引入缓存，先更新数据库再更新缓存。
        Map<String, Object> map = new HashMap<String, Object>(16);
        map.put("total", list.size());
        Integer success = 0;
        Integer fail = 0;
        Map<String, String> map1 = new HashMap<>(16);
        for (InventoryAddDto item : list) {
            String commodityId = item.getCommodityId();
            try {
                Integer integer = commodityMapper.updateCommodityInventory(commodityId, item.getCommodityInventory());
                //更新为0则不存在
                if (integer == 0) {
                    map1.put(commodityId, "缓存或数据库中不存在");
                    fail++;
                    continue;
                }
                success++;
            } catch (Exception e) {
                map1.put(commodityId, "添加程序执行失败，请检查数据完整性");
                fail++;
            }
        }
        map.put("success", success);
        map.put("fail", fail);
        map.put("failCommodityIdList", map1);
        return Result.OK(map);
    }
}
